package sealte.hvpms.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.UserDAO;
import sealte.hvpms.dao.impl.UserDAOImpl;

import com.google.gson.Gson;

public class Unregister extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String id_str = request.getParameter("id");
		if(id_str!=null){
			int id = Integer.parseInt(id_str);
			String user_path = this.getServletContext().getRealPath("") + "\\upload_Images"+"\\"+String.valueOf(id);
			File file = new File(user_path);
			if(file.exists()){
				String[] fileNames = file.list();
				File fileIn = null;
				for(int i=0;i<fileNames.length;i++){
					fileIn = new File(user_path+"/"+fileNames[i]);
					fileIn.delete();
				}
				file.delete();
			}
			UserDAO ub = new UserDAOImpl();
			boolean flag = ub.unregister(id);
			String message = "";
			if(flag){
				message = "ok"; 
			}else{
				message = "fail";
			}
			Gson gson = new Gson();
			String json = gson.toJson(message);
			out.write(json);
		}else{
			out.println("谢绝跨路径访问");
		}
	}

}
